use serde::{Deserialize, Serialize};

use base64::{engine::general_purpose, Engine as _};
use libp2p_identity::Keypair;

#[derive(Debug, Deserialize, Serialize, Clone)]
pub struct PeerConfig {
    pub peer_id: String,
    pub keypair: String,
}

impl std::default::Default for PeerConfig {
    fn default() -> Self {
        //生成默认配置
        let keypair = Keypair::generate_ed25519();

        let peer_id = keypair.public().to_peer_id().to_base58();
        let keypair_bytes = keypair.to_protobuf_encoding().map_or(vec![], |v| v);

        let encoded = general_purpose::STANDARD.encode(keypair_bytes);
        // keypair.to_protobuf_encoding()
        tracing::debug!("peer_id:{}", peer_id);
        tracing::debug!("keypair:{}", encoded);

        Self {
            peer_id: peer_id,
            keypair: encoded,
        }
    }
}
